/*
 * ThumbnailImageView.fx
 *
 * Created on 21-apr-2009, 10:05:24
 */

package fotoboek.assets;

/**
 * @author r.mohammadi
 */

import fotoboek.Main;
import fotoboek.model.model.Photo;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ThumbnailImageView extends CustomNode {
    public var photo: Photo;
    public var index: Integer;
    public var x: Number;
    public var y: Number;
    public var width: Number;
    public var height: Number;
    public var action:function()=null;
    override var translateX = bind x;
    override var translateY = bind y;

    public def bgRect = Rectangle {
        x: 0 y: 0 width: width height: height
        stroke: Color.BLACK
        fill: Color.RED
        visible: bind selected
    }

    public var selected: Boolean on replace {
        if(selected) {
            Main.description = Main.trimString("{photo.title}", 60);
        }
    }
    
    public var image: Image;

    def imageView: ImageView = ImageView {
        x: 2, y: 2
        smooth:false
        cache:false
        image:bind image              
    };

    override function create() : Node {
        Group {
            focusTraversable: false
            content: [ bgRect, imageView ]
        }
    }

    public override var onMousePressed = function(e: javafx.scene.input.MouseEvent) {
//        if(not Main.isDragging){
//           Main.selectedPhoto = photo.id;
//           println("Clicked on image {photo.id}");
//           Main.showFullPhoto(photo);
//
//           if(action != null){
//               action();
//           }
//        }
    }
        public override var onMouseReleased = function(e: javafx.scene.input.MouseEvent) {
        if(Main.isDragging == false and Main.currentScreen == 2){
           Main.currentScreen = 3;
           Main.selectedPhoto = photo.id;
           println("Clicked on image {photo.id}");
           Main.showFullPhoto(photo);

           if(action != null){
               action();
           }
        }
    }
}